package com.wzzc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.wzzc.bean.Student;
import com.wzzc.controller.UserController;

public class LoginInterceptor implements HandlerInterceptor{

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		Student user = (Student) request.getSession().getAttribute(UserController.SESSION_USER);
		if (user==null) {
			response.sendRedirect(request.getContextPath()+"/login.jsp");
			return false;
		}else if (request.getRequestURL().toString().contains("admin")) {
			if (!user.getStudentNo().equals("admin")) {
				response.sendRedirect(request.getContextPath()+"/login.jsp");
				return false;
			}
		}
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
	}

}
